home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-06-08 | 2.3 KB | 73 lines | [TEXT/GEOL] |
- Item 4194850 7-June-90 21:44PDT
-
- From: M.DANIEL Daniel Scientific, M Daniel,PRT
-
- To: CPLUS.APPLE$ C++ Interest List--Apple Employees
- CPLUS.DEV$ C++ Interest List--Developers
-
- Sub: Array of objects in a handle
-
- Hi! Guys and Gals,
-
- I have an array of objects in a handle. Here's how I do it.
-
- A temporary pointer (TP) is a pointer to a memory location allocated in the
- heap as a relocatable block. The rule is: a TP is only valid as long as memory
- is not moved.
-
- The problem is to get the C++ compiler to accept a TP as an object of some
- class. I overload the "new" operator like this:
-
- TP = new ( handle, offset ) class;
-
- to create the object. This version of "new" would simply deference the handle,
- add the offset to it, and return this result.
- Now there is a problem here. I have my TP and the compiler accepts it as an
- object, but the compiler will also call a constructor on my object everytime I
- use this version of new. This is good the first time I call "new", but bad
- thereafter. I'll re-initialize my object everytime I access it. The answer is
- to use constructor that does nothing. So the first time I access the object:
-
- TP = new ( handle, offset ) class( initMe );
-
- and thereafter:
-
- TP = new ( handle, offset ) class( ignore );
-
- So now I hide it all behind an array access interface.
-
- class objarray {
- public:
- obj& operator [] ( long );
- private:
- Handle h;
- };
-
- obj& objarray::operator[] ( long i ) {
-
- TP = new ( h, i ) obj(ignore);
-
- return *TP;
- }
-
- Add the rest of the details and Taa Daa!
- An array of objects in a handle, but accessed in source code like any array.
-
- For the index/offset, I used another class, arrayIndex. It allowed me to
- overload array index arithmetic to conform to the standard array index
- arithmetic. There is one more detail to mention. The dead space between array
- objects that allow them to all begin on long word boundaries. The size of an
- object may not the the distance between in an array.
-
- What do you think?
-
-
- Michael J. Daniel
- Daniel Scientific
- AppleLink: M.Daniel
-
-
- PS: You could also overload to call MoreMasters(), and allocate the array as
- individual handles, but I haven't tried it yet.
-
-